﻿// Extensions Made Easy - Copyright 2012 by Jan Van der Haegen - All rights reserved, use for educational purposes only.

using System;
using System.ComponentModel.Composition;
using ExtensionsMadeEasy.ClientAPI.Utilities.Base;
using Microsoft.LightSwitch.Client;

namespace ExtensionsMadeEasy.ClientAPI.Commands
{
    [InheritedExport(typeof(IEasyCommandExporter))]
    public abstract class EasyCommandExporter : NotifyPropertyChangedBase, ExtensionsMadeEasy.ClientAPI.Commands.IEasyCommandExporter
    {
        public EasyCommandExporter(
                                   string displayName,
                                   string description,
                                   string groupName,
                                   Uri imageUri,
                                   Dispatcher dispatcher = Dispatcher.PreferCurrentActiveScreen,
                                   bool useSmallImage = false,
                                   string prefixUsedForSortingWithinGroup = "")
        {
            this.displayName = displayName;
            this.description = description;
            this.groupName = groupName;
            this.imageUri = imageUri;
            this.ForceMainDispatcher = (dispatcher == Dispatcher.ForceMainDispatcher ? true : false);
            this.useSmallImage = useSmallImage;
            this.prefixUsedForSortingWithinGroup = prefixUsedForSortingWithinGroup;
        }

        private readonly bool useSmallImage;
        public bool UseSmallImage
        {
            get
            {
                return this.useSmallImage;
            }
        }
        private readonly string prefixUsedForSortingWithinGroup;
        public string PrefixUsedForSortingWithinGroup
        {
            get
            {
                return this.prefixUsedForSortingWithinGroup;
            }
        }

        public virtual bool IsCommandActiveForScreen(Microsoft.LightSwitch.Client.IScreenObject currentScreen)
        {
            return true;
        }

        private string displayName;

        public string DisplayName
        {
            get
            {
                return this.displayName;
            }
            set
            {
                this.displayName = value;
                this.OnPropertyChanged(() => this.displayName);
            }
        }

        private string description;

        public string Description
        {
            get
            {
                return this.description;
            }
            set
            {
                this.description = value;
                this.OnPropertyChanged(() => this.Description);
            }
        }

        private string groupName;

        public string GroupName
        {
            get
            {
                return this.groupName;
            }
            set
            { 
                this.groupName = value; 
                this.OnPropertyChanged(() => (this.GroupName)); 
            }
        }

        private Uri imageUri;

        public Uri ImageUri
        {
            get
            {
                return this.imageUri;
            }
            set
            { 
                this.imageUri = value; 
                this.OnPropertyChanged("Image"); 
            }
        }

        public bool ForceMainDispatcher { get; set; }
        
        public abstract void Execute(IScreenObject currentScreen);

        private bool canExecute = true;

        public bool IsEnabled

        {
            get
            {
                return this.canExecute;
            }
            set
            {
                this.canExecute = value;
                this.OnPropertyChanged(() => this.IsEnabled);
            }
        }
    }
}